const axios = require('axios')
const cheerio = require('cheerio');
// const fs = require('fs');
// const path = require('path');
// const interceptor = require('../config/interceptor')
const musicUrl = 'https://m.music.boycot.top'
// const musicUrl = 'http://192.168.5.105:3008/'
const tabs = [
    { label: 'api-docs', value: 'api', href: '/api-docs' },
    { label: '新闻', value: 'realtime' },
    { label: '电影', value: 'movie' },
    { label: '电视剧', value: 'teleplay' },
    { label: '壁纸', value: 'wallpaper' },
    { label: '音乐', value: 'music', href: musicUrl },
    { label: '小说', value: 'novel' },
    { label: '汽车', value: 'car' },
    {label: '游戏', value: 'game' },
]
const getData = ({ type, timer = true }) => {
    let dataList = []
    return axios.get('https://top.baidu.com/board?platform=pc&tab=' + type).then(result => {
        const $ = cheerio.load(result.data);
        $('.category-wrap_iQLoo').map((i, el) => {
            let types = $(el).find('.intro_1l0wp').eq(0).text().trim()
            let roles = $(el).find('.intro_1l0wp').eq(1).text().trim()
            types = types.split('：')[1] ? types.split('：')[1].split('、') : types
            roles = roles.split('：')[1] ? roles.split('：')[1].split('、') : roles
            const height = { realtime: 78, car: 88, game: 120 }
            let temp = {
                id: dataList.length + 1,
                type,
                height: height[type] || 160,
                title: $(el).find('.c-single-text-ellipsis').eq(0).text().trim() || undefined,
                img: $(el).find('img').attr('src') || undefined,
                types: (type !== 'car' ? type === 'novel' ? roles : types : roles) || undefined,
                roles: type !== 'car' ? (type === 'novel' ? types : roles) || undefined : undefined,
                price: type === 'car' ? types.join(' ') || undefined : undefined,
                isUp: Math.ceil(Math.random() * 100) > 50,
                points: $(el).find('.hide-icon').text().trim().split(' ')[0] || $(el).find('a').eq(1).text().trim().split(' ')[0] || undefined,
                desc: $(el).find('.c-single-text-ellipsis, .large_nSuFU').eq(1).text().trim().split(' ')[0] || undefined
            }
            temp && ((temp.desc && temp.desc.length > 5) || type === 'car') && dataList.push(temp)
        })
        return dataList
    })
}
const service = {
    getList (ctx) {
       let type = ctx.query.type || 'realtime'
        // 'movie', 'car', 'realtime', 'novel', 'teleplay'
        if (!tabs.map(el => el.value).includes(type)) {
            ctx.body = {}
            return ctx.body
        }
        return getData({ type, timer: false }).then(dataList => {
            return service.render({ ctx, dataList, type })
        })
    },
    getDetail (ctx, next) {
        let current = tabs.filter(_ => _.value == ctx.query.type)[0]
        dataList = JSON.parse(dataList)
        let data = dataList.filter(el => el.id == ctx.query.id)[0] || {}
        data.realImg = data.img.replace(/w_160,h_214/gi, 'w_500,h_680')
        data.realImg = data.img.replace(/w_256,h_170/gi, 'w_1920,h_1080')
        // console.log(data, 'req.query');
        res.render('detail', { pageTitle: (current.label || "") + "详情", data, query: ctx.query })
    },
    getData,
    tabs,
    render ({ ctx, dataList, type }) {
        let current = tabs.filter(el => el.value === type)[0] || {}
        current = current.label || ''
        return { pageTitle: "热搜排行榜-" + current, dataList, tabs, query: ctx.query }
    }
}
module.exports = service